local Protect_skillEffect = fk.CreateSkill {
  name = "#Protect_skillEffect",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["#Protect_skill_times_white"] = "博丽灵梦：%from 已经发动%arg次【%arg2】了，其将在第40次发动时被封魔！",
  ["#Protect_skill_times"] = "博丽灵梦：%from 的技能发动的实在是太频繁了，封魔全部技能！",
}

local white_package_extensionName = {"shzl","standard_ex","yj","sp","ol","tenyear","mougong","mobile","mini"}

Protect_skillEffect:addEffect(fk.SkillEffect, {
  mute = true,
  global = true,
  priority = 100,
  can_trigger = function(self, event, target, player, data)
    return target == player and data.skill:getSkeleton() and data.skill:isPlayerSkill(target) and target:hasSkill(data.skill:getSkeleton().name)
    and target:usedSkillTimes(data.skill:getSkeleton().name, Player.HistoryPhase) >= 30
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local name = data.skill:getSkeleton().name
    if table.contains(white_package_extensionName, data.skill.package.extensionName) then
      local num = target:usedSkillTimes(name, Player.HistoryPhase)
      room:sendLog{ type = "#Protect_skill_times_white", from = player.id, arg = num, arg2 = Fk:translate(name), toast = true }
    end
    if not table.contains(white_package_extensionName, data.skill.package.extensionName) or player:usedSkillTimes(name, Player.HistoryPhase) >= 40 then
      room:sendLog{ type = "#Protect_skill_times", from = player.id, toast = true }
      local mark = room:getBanner(Protect_skillEffect.name..player.id) or {}
      for _, skill in ipairs(player.player_skills) do
        if skill:isPlayerSkill(player) and skill.visible then
          table.insertIfNeed(mark, skill.name)
        end
      end
      room:handleAddLoseSkills(player, "-" .. table.concat(mark, "|-"), nil, false, true)
      room:setBanner(Protect_skillEffect.name..player.id, mark)
      room:setPlayerMark(player, "@!Protect_skillEffect-phase", 1)
    end
  end
})

Protect_skillEffect:addEffect(fk.EventPhaseEnd, {
  mute = true,
  global = true,
  priority = 100,
  can_trigger = function(self, event, target, player, data)
    return target and table.find(target.room.alive_players, function(p) return target.room:getBanner(Protect_skillEffect.name..p.id) ~= nil end)
  end,
  on_use = function(self, event, target, player, data)
    local room = target.room
    for _, p in ipairs(room.alive_players) do
      if room:getBanner(Protect_skillEffect.name..p.id) ~= nil then
        local mark = room:getBanner(Protect_skillEffect.name..p.id)
        room:handleAddLoseSkills(p, table.concat(mark,"|"), nil, false, true)
        room:setBanner(Protect_skillEffect.name..p.id, nil)
        room:setPlayerMark(p, "@!Protect_skillEffect-phase", 0)
      end
    end
  end
})

return Protect_skillEffect
